package BVRClient;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 * The Blue vs Red: Arena Player Class.
 * @author Jesse Gill
 * @author Shannon Setter
 */
public class BVRC_Player extends BVRC_Entity
{
	public enum PLAYER_TEAM
	{
		RED, BLUE
	}
	
	private String name = "";
	
	private int movementSpeedX = 0;
	private int movementSpeedY = 0;
	
	private int playerID = -1;
	
	private PLAYER_TEAM team = PLAYER_TEAM.RED;
	
	private BVRC_Rect tempRect = new BVRC_Rect(0, 0, 0, 0);
	private BVRC_Point bulletStartPosition = new BVRC_Point(17, 1);
	
	private Image sprite = null;
	
	/**
	 * 
	 */
	public BVRC_Player() {}
	
	/**
	 * 
	 * @param camera
	 */
	public void draw(BVRC_Camera camera)
	{
		if (this.sprite == null)
			return;
		
		this.sprite.draw(this.entityRect.x - camera.getVisibleRect().x, this.entityRect.y - camera.getVisibleRect().y);
	}
	
	/**
	 * 
	 * @param delta
	 * @return true if the movement was successful, false in all other cases
	 */
	public boolean updateMovement(int index, int delta, BVRC_Map currentMap)
	{
		if (this.movementSpeedX == 0 && this.movementSpeedY == 0)
			return false;
		
		this.tempRect.x = this.entityRect.x;
		this.tempRect.y = this.entityRect.y;
		
		float velocityX = (float)this.movementSpeedX * (float)(delta / 1000.0f);
		float velocityY = (float)this.movementSpeedY * (float)(delta / 1000.0f);
		
		this.entityRect.x += velocityX;
		this.entityRect.y -= velocityY;
		
		if (currentMap.rectMapCollision(this.entityRect))
		{
			this.entityRect.x = this.tempRect.x;
			this.entityRect.y = this.tempRect.y;
			
			return false;
		}
		
		return true;
	}
	
	/**
	 * 
	 * @param p
	 * @param camera
	 */
	public void rotateFacePoint(BVRC_Point p, BVRC_Camera camera)
	{
		double alpha = Math.atan2(p.y - (this.entityRect.y - camera.getVisibleRect().y), p.x - (this.entityRect.x - camera.getVisibleRect().x));
		
		if (this.sprite != null)
			this.sprite.setRotation((float) (alpha * 180 / Math.PI) + 90);
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * 
	 * @return the player's name
	 */
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * 
	 * @param sprite
	 */
	public void setSprite(Image sprite)
	{
		this.sprite = sprite;
		
		this.entityRect.width = this.sprite.getWidth();
		this.entityRect.height = this.sprite.getHeight();
	}
	
	/**
	 * 
	 * @param filename
	 */
	public void setSprite(String filename)
	{
		try
		{
			this.sprite = new Image(filename);
			
			this.entityRect.width = this.sprite.getWidth();
			this.entityRect.height = this.sprite.getHeight();
		}
		catch(SlickException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 */
	public void setSpriteFromTeam()
	{
		switch (this.team)
		{
			case BLUE:
				if (BVRC_ImageResources.blueTeamSprite != null)
					this.sprite = BVRC_ImageResources.blueTeamSprite.copy();
			break;
			
			case RED:
				if (BVRC_ImageResources.redTeamSprite != null)
					this.sprite = BVRC_ImageResources.redTeamSprite.copy();
			break;
		}
		
		if (this.sprite != null)
		{
			this.entityRect.width = this.sprite.getWidth();
			this.entityRect.height = this.sprite.getHeight();
		}
	}
	
	/**
	 * 
	 * @return the player's sprite image
	 */
	public Image getSprite()
	{
		return sprite;
	}
	
	/**
	 * 
	 * @param team
	 */
	public void setTeam(PLAYER_TEAM team)
	{
		this.team = team;
	}
	
	/**
	 * 
	 * @return the player's team
	 */
	public PLAYER_TEAM getTeam()
	{
		return team;
	}
	
	/**
	 * 
	 * @param movementSpeedX
	 */
	public void setMovementSpeedX(int movementSpeedX)
	{
		this.movementSpeedX = movementSpeedX;
	}
	
	/**
	 * 
	 * @return the player's current X axis movement speed
	 */
	public int getMovementSpeedX()
	{
		return movementSpeedX;
	}
	
	/**
	 * 
	 * @param movementSpeedY
	 */
	public void setMovementSpeedY(int movementSpeedY)
	{
		this.movementSpeedY = movementSpeedY;
	}
	
	/**
	 * 
	 * @return the player's current Y axis movement speed
	 */
	public int getMovementSpeedY()
	{
		return movementSpeedY;
	}
	
	/**
	 * 
	 * @param rotation
	 */
	public void setRotation(float rotation)
	{
		if (sprite == null)
			return;
		
		sprite.setRotation(rotation);
	}
	
	/**
	 * 
	 * @return the player's rotation angle
	 */
	public float getRotation()
	{
		if (sprite == null)
			return 0;
		
		return sprite.getRotation();
	}
	
	/**
	 * 
	 * @param playerID
	 */
	public void setPlayerID(int playerID)
	{
		this.playerID = playerID;
	}
	
	/**
	 * 
	 * @return the player's current ID
	 */
	public int getPlayerID()
	{
		return playerID;
	}

	public void setBulletStartPosition(BVRC_Point bulletStartPosition)
	{
		this.bulletStartPosition = bulletStartPosition;
	}

	public BVRC_Point getBulletStartPosition()
	{
		return bulletStartPosition;
	}
}
